import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.util.Enumeration;
import java.util.Properties;


public class GiveReply {
	Properties replies = new Properties(); 
	Properties randomReplies = new Properties();
	Properties regularExpressions = new Properties();
	int maxRandom=0;
public GiveReply(){
	try { 
		replies.load(new FileInputStream("/home/akshat/Jworkspace/jabber/src/replies.properties")); 
		randomReplies.load(new FileInputStream("/home/akshat/Jworkspace/jabber/src/random.properties"));
		regularExpressions.load(new FileInputStream("/home/akshat/Jworkspace/jabber/src/regularexpressions.properties"));
		System.out.println("testing the reply for hi = "+ replies.getProperty("hi"));
		File file = new File("/home/akshat/Jworkspace/jabber/src/random.properties");

	      if (file.exists()){
			      FileReader fr = new FileReader(file);
			      LineNumberReader ln = new LineNumberReader(fr);
			      int count = 0;
			      while (ln.readLine() != null)
			          count++;
			      maxRandom=count;
	      }
	} catch (IOException e) { 
		System.out.println("Could not read replies.properties File");
	} 
	
	
}
public String response(String text){
	String temp="";
	boolean found=false;
	String []arr=text.split(" ");
	Enumeration e = regularExpressions.propertyNames();
	
	while (e.hasMoreElements()) {
	      String key = (String) e.nextElement();
	      if(text.toLowerCase().matches(key))
	      {
	      temp=regularExpressions.getProperty(key);
	      found=true;
	      break;
	      }
	    }
	if(found==false)
	{
			if(replies.getProperty(text.toLowerCase())!=null)
				temp=temp+replies.getProperty(text.toLowerCase());
			else
				for(String word:arr){
					if(replies.getProperty(word.toLowerCase())!=null)
					{
						temp=temp+" "+replies.getProperty(word.toLowerCase());
						System.out.println(word+ " = "+replies.getProperty(word.toLowerCase()));
					}
				}
	}
	
	return temp;
}

public String randomResponse(){
	int i=((int)( maxRandom * Math.random()));
	return randomReplies.getProperty((new Integer(i)).toString());
}
}
